package cn.yunlang.zhangZhongFei.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import cn.yunlang.zhangZhongFei.service.UserDislikeService;
import cn.yunlang.zhangZhongFei.util.JwtUtil;
import cn.yunlang.zhangZhongFei.vo.request.TokenRequest;
import cn.yunlang.zhangZhongFei.vo.request.UserDislikePageVo;
import cn.yunlang.zhangZhongFei.vo.request.UserDislikeVo;
import cn.yunlang.zhangZhongFei.vo.response.R;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/UserDislike")
@Api(tags = "用户拉黑记录")
public class UserDislikeController {

    @Resource
    private UserDislikeService userDislikeService;

    @ApiOperation("分页查")
    @GetMapping("/page")
    public R<IPage<UserDislikePageVo>> page(
            @RequestParam Integer pageNo,
            @RequestParam Integer pageSize,
            @RequestBody TokenRequest request) {
        Claims claims = new JwtUtil().parseJWT(request.getToken());
        Long userId = Long.valueOf(claims.getSubject());
        return userDislikeService.pageDislike(pageNo, pageSize, userId);
    }

    @PostMapping("/doDislike")
    @ApiOperation("拉黑or取消拉黑")
    public R<String> doDislike(
            @RequestBody UserDislikeVo userDislike) {
        userDislike.setUserId(Long.valueOf(new JwtUtil().getUserId()));
        return userDislikeService.doDislike(userDislike);
    }

    @PostMapping("/dislike")
    @ApiOperation("拉黑")
    public R<String> dislike(
            @RequestBody UserDislikeVo userDislike) {
        userDislike.setUserId(Long.valueOf(new JwtUtil().getUserId()));
        return userDislikeService.dislikeUser(userDislike);
    }

    @PostMapping("/removeDislike")
    @ApiOperation("取消拉黑")
    public R<String> removeDislike(
            @RequestBody UserDislikeVo userDislike) {
        userDislike.setUserId(Long.valueOf(new JwtUtil().getUserId()));
        return userDislikeService.removeDislikeUser(userDislike);
    }
}
